package com.mymarkdown.app.controller;

import com.mymarkdown.app.common.ResponseResult;
import com.mymarkdown.app.interfaces.ModifyWindow;
import com.mymarkdown.app.interfaces.UserInfoService;
import com.mymarkdown.app.model.UserSettingInfo;
import com.mymarkdown.app.service.SystemSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author zenglun
 * @date 2021/12/15 10:15 上午
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/system")
public class SystemSettingController {

    @Autowired
    private SystemSettingService systemSettingService;

    @Autowired
    private ModifyWindow modifyWindow;
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/setSystemSetting")
    public ResponseResult setSystemSetting(@RequestBody UserSettingInfo userSettingInfo){
        log.info("[SystemSettingController]---[setSystemSetting],start execute , params:{}", userSettingInfo);
        if (Objects.isNull(userSettingInfo)){
            return ResponseResult.error();
        }
        return systemSettingService.setSystemSetting(userSettingInfo);
    }

    @GetMapping("/getSystemSetting")
    public ResponseResult getSystemSetting(){
        try {
            UserSettingInfo userSettingInfo = systemSettingService.getOriginalInfo();
            return ResponseResult.ok(userSettingInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[SystemSettingController]---[getSystemSetting]发生异常,异常信息为:{}", e);
        }
        return ResponseResult.error();
    }

    @GetMapping("/setCurrentOperationFileSaveState")
    public ResponseResult setCurrentOperationFileSaveState(@RequestParam Boolean state){
        userInfoService.setFileSaveState(state);
        modifyWindow.changeTitle();
        return ResponseResult.ok("");
    }
}
